package com.example.goods.controller;

import com.example.adminuser.dto.GoodsPage;
import com.example.goods.entity.Goods;
import com.example.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 11:29 2019/4/1
 */
@Controller
public class GoodsController {

    @Autowired
    @Qualifier("goodsServiceImpl")
    private GoodsService goodsService;


    @RequestMapping("/publish")
    public String publishGoods(){
        return "publish";
    }
    @RequestMapping("/publishCollect")
    public String publishCollectGoods(){
        return "publishCollect";
    }
    @RequestMapping("/collectGoodslist")
    public String collectGoodslist(Model model,@RequestParam(value = "offset",required = false) Integer offset){
        if (offset==null){
            offset=0;
        }
        List<Goods> goodsList=goodsService.collectGoodsPaginationList(offset);
        int count =goodsService.collectGoodsListCount();
        int currentPage;
        if (offset==0){
            currentPage=1;
        }else {
            currentPage=offset;
        }
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("allPage",count);
        model.addAttribute("goodsList",goodsList);
        return "collectGoodslist";
    }
    @RequestMapping("/goodslist")
    public String goodsList(Model model,@RequestParam(value = "offset",required = false) Integer offset){
        if (offset==null){
            offset=0;
        }
        List<Goods> goodsList=goodsService.goodsPaginationList(offset);
    int count =goodsService.goodsListCount();
    int currentPage;
    if (offset==0){
      currentPage=1;
    }else {

        currentPage=offset;
    }
    model.addAttribute("currentPage",currentPage);
    model.addAttribute("allPage",count);
    model.addAttribute("goodsList",goodsList);
        return "goodslist";
    }

    @RequestMapping("/goodsDetail/{goodsId}")
    public String goodDetail(@PathVariable("goodsId") Long goodsId,Model model){
        Goods goods=goodsService.selectGoodsDetail(goodsId);
         model.addAttribute("good",goods);
        return "gooddetail";
    }
    @RequestMapping("/collectGoodsDetail/{goodsId}")
    public String collectGoodsDetail(@PathVariable("goodsId") Long goodsId,Model model){
        Goods goods=goodsService.selectGoodsDetail(goodsId);
        model.addAttribute("good",goods);
        return "collectGooddetail";
    }
    @PostMapping("/publistpost")
    @ResponseBody
    public String publish(Goods goods){
        goodsService.publishPost(goods);
        return "succ";
    }
    @PostMapping("/publistcollectpost")
    @ResponseBody
    public String publishCollect(Goods goods){
        goodsService.publishCollectPost(goods);
        return "succ";
    }
    @GetMapping("/goodsList")
    @ResponseBody
    public GoodsPage goodsPageList(int offset,int limit){
        GoodsPage goodsPage=new GoodsPage();
        List<Goods> goodsList=goodsService.selectGoodsListPage(offset,limit);
        int count=goodsService.selectGoodsPageCount();
        goodsPage.setTotal(count);
        goodsPage.setRows(goodsList);
        return goodsPage;
    }
}
